package ClassifierTraining;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import thesis.InputTweetInfoExtractor;


import com.mongodb.DBObject;


public class ClassifierDialog extends JDialog implements ActionListener{
	private static final long serialVersionUID = 1L;
	
	private JCheckBox[] positiveChecks;
	private JCheckBox[] negativeChecks;
	private JButton next;
	
	private List<DBObject> positiveSamples = new ArrayList<DBObject>();
	private List<DBObject> negativeSamples = new ArrayList<DBObject>();
	
	private List<DBObject> tweets;
	private int tweetsSize;
	
	public ClassifierDialog(String concept, List<DBObject> tweets){
		this.tweets = tweets;
		tweetsSize = tweets.size();
		
		this.setModal(true);
		
		setBounds(0,0, GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width, GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		
		Container c = this.getContentPane();
		c.setLayout(new GridLayout(2 + tweetsSize, 1));
		
		JPanel panelTitle = new JPanel();
		panelTitle.setLayout(new GridLayout(1,1));
		JLabel title = new JLabel(concept.toUpperCase());
		Font fontTitle = new Font("Arial", Font.BOLD, 40);
		title.setFont(fontTitle);
		title.setHorizontalAlignment(JLabel.CENTER);
		panelTitle.add(title);
		
		c.add(panelTitle);
		
		positiveChecks = new JCheckBox[tweetsSize];
		negativeChecks = new JCheckBox[tweetsSize];
		for (int i=0; i<tweetsSize; i++){
			JPanel panelTweet = new JPanel();
			panelTweet.setLayout(new GridLayout(2,1));
			DBObject tweet = tweets.get(i);
			InputTweetInfoExtractor infoExtr = new InputTweetInfoExtractor(tweet);
			JTextField tweetText = new JTextField(infoExtr.getText());
			tweetText.setEditable(false);
			Font fontTweets = new Font("Times New Roman", Font.PLAIN, 20);
			tweetText.setFont(fontTweets);
			JPanel panelCheck = new JPanel();
			panelCheck.setLayout(new GridLayout(1, 2));
			JPanel panelPositive = new JPanel();
			JLabel positiveLabel = new JLabel("Positive sample?");
			positiveLabel.setFont(fontTweets);
			positiveChecks[i] = new JCheckBox();
			positiveChecks[i].addActionListener(this);
			panelPositive.add(positiveLabel);
			panelPositive.add(positiveChecks[i]);
			
			JPanel panelNegative = new JPanel();
			JLabel negativeLabel = new JLabel("Negative sample?");
			negativeLabel.setFont(fontTweets);
			negativeChecks[i] = new JCheckBox();
			negativeChecks[i].addActionListener(this);
			panelNegative.add(negativeLabel);
			panelNegative.add(negativeChecks[i]);
			
			if(i % 2 == 0){
				tweetText.setBackground(Color.white);
				panelPositive.setBackground(Color.white);
				panelNegative.setBackground(Color.white);
			}
			else{
				tweetText.setBackground(Color.lightGray);
				panelPositive.setBackground(Color.lightGray);
				panelNegative.setBackground(Color.lightGray);
			}
			
			panelCheck.add(panelPositive);
			panelCheck.add(panelNegative);
			
			panelTweet.add(tweetText);
			panelTweet.add(panelCheck);
			
			c.add(panelTweet);
		}
		
		JPanel panelNext = new JPanel();
		next = new JButton("next");
		next.addActionListener(this);
		panelNext.add(next);
			
		c.add(panelNext);
	}
	
	public List<DBObject> getPositiveSamples() {
		return positiveSamples;
	}
	
	public List<DBObject> getNegativeSamples() {
		return negativeSamples;
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == next){
			for(int i=0; i<tweetsSize; i++){
				if (positiveChecks[i].isSelected()){
					positiveSamples.add(tweets.get(i));
				}
			}
			for(int i=0; i<tweetsSize; i++){
				if (negativeChecks[i].isSelected()){
					negativeSamples.add(tweets.get(i));
				}
			}
			
			this.dispose();
		}
		
		for(int i=0; i<tweetsSize; i++){
			if (e.getSource()==positiveChecks[i] && positiveChecks[i].isSelected()){
				if (negativeChecks[i].isSelected()){
					negativeChecks[i].setSelected(false);
				}
			}
		}
		
		for(int i=0; i<tweetsSize; i++){
			if (e.getSource()==negativeChecks[i] && negativeChecks[i].isSelected()){
				if (positiveChecks[i].isSelected()){
					positiveChecks[i].setSelected(false);
				}
			}
		}
	}
}
